package com.aji.wineserver.controller;

import com.aji.winecommon.result.Result;
import com.aji.winecommon.utills.SpectrumUtill;
import com.aji.winepojo.vo.WinePreVo;
import com.aji.winepojo.vo.WineVo;
import com.aji.winepojo.vo.WineVoPlus;
import com.aji.wineserver.config.FileEnvConfig;
import com.aji.wineserver.domain.ModelData;
import com.aji.wineserver.service.ModelDataService;
import com.aji.wineserver.service.ModelService;
import com.aji.wineserver.service.WineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "模型运行接口")
@RequestMapping("/model")
@Slf4j
public class ModelController {
    @Autowired
    private ModelService modelService;
    @Autowired
    private WineService wineService;
    @Autowired
    private FileEnvConfig fileEnvConfig;
    @Autowired
    private ModelDataService modelDataService;

    @ApiOperation("训练模型")
    @GetMapping("/trainModel")
    public Result trainWine(Integer modelId) throws IOException, InterruptedException {
        ModelData modelData = modelDataService.getById(modelId);
        modelService.trainWine(modelData.getModelPath());
        return Result.success();
    }

    @ApiOperation("识别")
    @GetMapping("/identModel/{ids}")
    public Result identWine(Integer dataType,  @PathVariable List<Integer> ids,Integer modelId) throws IOException, InterruptedException {
        //查询模型
        ModelData modelData = modelDataService.getById(modelId);
        //查询样本
        List<WineVo> wineVo = wineService.listByIds(ids);
        //运行结果
        List<WinePreVo> wineVoPluses = modelService.identWine(wineVo,modelData);
        for (WinePreVo wineVoPlus:wineVoPluses
             ) {
            wineVoPlus.setSpectrumX(SpectrumUtill.getX(fileEnvConfig.getDataPath()+fileEnvConfig.getDemoFile()));
        }
        //修改状态
        wineService.changeIdentfication(ids);
        return Result.success(wineVoPluses);
    }
    @ApiOperation("查询 模型列表")
    @GetMapping("/list")
    public Result listModel(){return Result.success(modelDataService.list());}
    @ApiOperation("修改 模型路径 为了线上好部署")
    @PostMapping("/modelPath")
    public Result modifyPath(@RequestBody ModelData modelData){
        modelDataService.updateById(modelData);
        return Result.success();
    }
}
